package lookas;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MovieInfo{
    final private static Logger log = LoggerFactory.getLogger(MovieInfo.class);

    private Object loadPage(String url, ParserCallback parser)
        throws HttpException, IOException{

        HttpClient client;
        client = new HttpClient();
        client.getHttpConnectionManager().getParams().setConnectionTimeout(
            30000);

        GetMethod get = new GetMethod(url);
        get.setFollowRedirects(true);
        client.executeMethod(get);
        InputStream stream = get.getResponseBodyAsStream();
        if(stream == null)
            return null;
        BufferedReader r = new BufferedReader(new InputStreamReader(stream));
        return parser.parse(r);
    }

    private interface ParserCallback{
        public Object parse(BufferedReader r) throws IOException;
    }

    class SimplePrintParser implements ParserCallback{

        public Object parse(BufferedReader r) throws IOException{
            String input;
            while((input = r.readLine()) != null){
                System.out.println(input);
            }
            return null;
        }
    }

    class DataCallback implements ParserCallback{
        public Object parse(BufferedReader r) throws IOException{
            String ret = "";
            String tmp = "";
            String input;
            boolean w = false;
            while((input = r.readLine()) != null){
                if(input.indexOf("<div id=\"tn15lhs\">") >= 0)
                    w = true;
                if(w){
                    tmp += input;
                    if(input.indexOf("<div class=\"bottom\">") >= 0){
                        w = false;
                        break;
                    }
                }
            }
            {
                int in = tmp.indexOf("<div id=\"tn15title\"");
                String s = "</div>";
                int out = tmp.indexOf(s, in) + s.length();
                ret += tmp.substring(in, out);
            }
            {
                String s1 = "<div id=\"tn15rating\"";
                int in1 = tmp.indexOf(s1);
                int in2 = tmp.indexOf("</b>", in1);
                int in = tmp.indexOf("<b>", in2);
                String s2 = "</b>";
                int out = tmp.indexOf(s2, in) + s2.length();
                ret += tmp.substring(in, out);
            }
            {
                int in = tmp.indexOf("<div class=\"photo\"");
                int in1 = tmp.indexOf("img", in);
                int in2 = tmp.indexOf("src", in1);
                int in3 = tmp.indexOf(".", in2);
                int out = tmp.indexOf("\"", in3) + 1;
                ret += tmp.substring(in, out) + " /></a></div>";
            }
            return ret;
        }
    }

    class DescriptionCallback implements ParserCallback{
        public Object parse(BufferedReader r) throws IOException{
            String ret = "";
            String input;
            boolean w = false;
            String s = "<p class=\"plotpar\"";
            while((input = r.readLine()) != null){
                if(input.indexOf(s) >= 0)
                    w = true;
                if(w){
                    ret += input;
                    if(input.indexOf("</p>") >= 0)
                        w = false;
                }
            }
            if(ret.length()>0){int x = ret.indexOf(s) + s.length() + 1;
            int y = ret.lastIndexOf("</p>");
            ret = ret.substring(0, x) + "<![CDATA[" + ret.substring(x, y)
                + "]]>" + ret.substring(y);
            return ret;}
            else
            	return "<p>Empty</p>";
        }
    }

    class GugleCallback implements ParserCallback{
        public Object parse(BufferedReader r) throws IOException{
            String input;
            String u = "/title/";
            while((input = r.readLine()) != null){                
                int sta = input.indexOf(u);
                if(sta > 0){
                    int sto = input.indexOf("/", sta + u.length());
                    return "http://www.imdb.com" + input.substring(sta, sto);
                }
            }
            return null;
        }
    }

    public String getMovieLink(String title) throws HttpException, IOException{
        String url = "http://www.google.pl/search?num=1&q=site:imdb.com/title+"
            + title;
        // String url = "http://www.imdb.com/find?s=tt&q=" + title;
        log.info(url);
        return (String)loadPage(url, new GugleCallback());
    }

    public String getData(String link) throws HttpException, IOException{
        String page = "<root src=\"" + link + "\">"
            + (String)loadPage(link, new DataCallback());
        page += (String)loadPage(link + "/plotsummary",
            new DescriptionCallback())
            + "</root>";
        return page;
    }
}
